<?php
defined('JOC') or die;
/*-------------------------------------------------------|
 * Model Class.                                          +
 * ------------------------------------------------------|
 * Description :                                         +
 * It contains common methos for new models classes.     +
 * ------------------------------------------------------|
 */
class Model {
  /*-------------------------------------------------------|
   * DataBase table name.                                  +
   * ------------------------------------------------------|
   * Description :                                         +
   * The name of the table that this model will control    +
   * ------------------------------------------------------|
   */
  protected $_table_name  = '' ;
  /*-------------------------------------------------------|
   * Primary Key.                                          +
   * ------------------------------------------------------|
   * Description :                                         +
   * Primary Key DataBase table name                       +
   * ------------------------------------------------------|
   */
  protected $_pk_name     = '' ;
  /*-------------------------------------------------------|
   * Last Query Result.                                    +
   * ------------------------------------------------------|
   * Description :                                         +
   * Array of assoc rows.                                  +
   * ------------------------------------------------------|
   */
  protected $_last_result = Array() ;
  /*-------------------------------------------------------|
   * Last Query Result.                                    +
   * ------------------------------------------------------|
   * Description :                                         +
   * Array of assoc rows.                                  +
   * ------------------------------------------------------|
   */
  private $__defided_db = FALSE ;
  
  function __construct()
	{
    global $db ;
    $this->__defided_db = isset($db) && $db->conected() ? TRUE : FALSE ;
	}
  function SelectElementById($content_id = 0)
  {
    $sql = " SELECT * " ;
		$sql .= " FROM {prefix}{$this->_table_name}  " ;
		$sql .= " WHERE {prefix}{$this->_pk_name} =  {$content_id} LIMIT 1 ; " ;
    
    $this->_last_result = $this->db()->query($sql) ;
  }
  function SelectElementsByModuleInstance($module_id = 0, $module_instance_id = 0)
  {
   	$sql = " SELECT TN.* " ;
		$sql .= " FROM {prefix}module_instance_content AS MIC " ;
		$sql .= " INNER JOIN {prefix}{$this->_table_name} AS TN ON MIC.content_id = TN.{$this->_pk_name} " ;
		$sql .= " WHERE MIC.module_instance_id = {$module_instance_id} AND content_module_id = {$module_id} ; " ;

    $this->_last_result = $this->db()->query($sql) ;
  }
  function Select($limit = 0, $orderby = '', $order = '' )
  {
    $sql = " SELECT * FROM {prefix}{$this->_table_name} " ;
    $sql .= $limit ? " LIMIT {$limit} " : "" ;
    $sql .= $orderby && $order ? " ORDER BY {$orderby} {$order} " : "" ;
    $sql .=  " ; " ;
    $this->_last_result = $this->db()->query($sql) ;
  }
  protected function db()
  {
    global $db ;
    return $this->__defided_db ? $db : FALSE ;
  }
  
  function get()
  {
    return $this->_last_result ;
  }
  function num_rows()
  {
    return count($this->_last_result) ;
  }
}
/*
 * ---------------------------------------------------------+
 * End of file model.class.php                              |
 * ---------------------------------------------------------+
 * located at core/model/model.class.php                    |
 * ---------------------------------------------------------+
*/